// 界面显示时，显示用户信息
window.onload = function() {
    const nicknameElement = document.querySelector('#user');
    const nickname = sessionStorage.getItem('nickname');
    if (nickname) {
        nicknameElement.textContent = nickname;
    } else {
        nicknameElement.textContent = "登录|注册";
        return;
    }
    $.ajax({
        // 请求
        url: "/index",
        method: "get",
        data: {nickname: nickname},
        dataType:"text",
        success: function(dataString) {
            const data = JSON.parse(dataString);
            const contentElement = document.querySelector('.user-info-box');
            contentElement.innerHTML =
                '<div class="image">' +
                '<img src="' + data.avatar + '" alt="头像" id="userImage">' +
                '</div>' +
                '<div class="user-info">' +
                '<table style="margin-left: 5%">'+
                '<tr>' +
                '<td id="welcome" height="30px">欢迎您，</td>' +
                '<td id="username" height="30px">' + data.name + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td height="30px">账号名：</td>' +
                '<td height="30px">' + data.account + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td height="30px">余额：</td>' +
                '<td height="30px">' + data.balance + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td colspan="2" height="30px"><a href="login.html">退出登录</a></td>' +
                '</table>'+
                '</div>';
        sessionStorage.setItem('balance', data.balance);
        sessionStorage.setItem('account', data.account);
        sessionStorage.setItem('userid',data.id);
        sessionStorage.setItem('image',data.avatar);
        }
    })
};

// 显示商品
setTimeout(function() {
    $.ajax({
        url: "/goods",
        method: "get",
        data: {num: 1},
        dataType: "json",
        success: function(dataString) {
            show(dataString);
        }
    });
}, 1000); // 延迟1秒

// 简易倒计时逻辑
function updateCountdown() {
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();
    const currentTimeInSeconds = hours * 3600 + minutes * 60 + seconds;

    let nextSeckillStart = Math.floor(hours / 2) * 2 * 3600;
    if (currentTimeInSeconds >= nextSeckillStart + 3600) {
        nextSeckillStart += 2 * 3600;
    }

    const seckillEnd = nextSeckillStart + 3600;
    const timeRemaining = seckillEnd - currentTimeInSeconds;
    const nextSeckillIn = nextSeckillStart - currentTimeInSeconds;

    const statusElement = document.getElementById('status');
    const timeElement = document.getElementById('time');
    const goodsInfoBox = document.getElementById("goods-info-box")

    if (currentTimeInSeconds >= nextSeckillStart && currentTimeInSeconds < seckillEnd) {
        statusElement.textContent = '正在秒杀';
        timeElement.textContent = `剩余时间: ${Math.floor(timeRemaining / 3600)}小时 ${Math.floor((timeRemaining % 3600) / 60)}分钟 ${timeRemaining % 60}秒`;
        goodsInfoBox.style = "background-color: darkred;"
    } else {
        statusElement.textContent = '距下一场开始';
        timeElement.textContent = `${Math.floor(nextSeckillIn / 3600)}小时 ${Math.floor((nextSeckillIn % 3600) / 60)}分钟 ${nextSeckillIn % 60}秒`;
        goodsInfoBox.style = "background-color: grey;"
    }
}

setInterval(updateCountdown, 1000);
updateCountdown();

